#include "VSettings.h"
#include "ui_VSettings.h"
#include "CSettings.h"
#include <QMessageBox>
#include <QDebug>
#include "CUIHelper.h"
#include "CMainWindow.h"

VSettings::VSettings(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::VSettings)
{
    ui->setupUi(this);

    m_pSetting = CSettings::getInstance()->settings();
    readSettings();
}

VSettings::~VSettings()
{
    delete ui;
}

void VSettings::readSettings()
{
    ui->spinBox_port->setValue(m_pSetting->value("Setting/DefaultPort").toInt());
    ui->comboBox_text_code->setCurrentText(m_pSetting->value("Setting/TextCode").toString());
    ui->spinBox_maxline->setValue(m_pSetting->value("Setting/MaxLine").toInt());
    ui->spinBox_span->setValue(m_pSetting->value("Setting/Span").toInt());

    QString strLanguage = m_pSetting->value("Setting/Language").toString();
    if("简体中文" == strLanguage) {
        ui->radioButton_zn->setChecked(true);
    } else if("English" == strLanguage) {
        ui->radioButton_english->setChecked(true);
    }
}

void VSettings::writeSettings()
{
    m_pSetting->setValue("Setting/DefaultPort",ui->spinBox_port->value());
    m_pSetting->setValue("Setting/TextCode",ui->comboBox_text_code->currentText());
    m_pSetting->setValue("Setting/MaxLine",ui->spinBox_maxline->value());
    m_pSetting->setValue("Setting/Span",ui->spinBox_span->value());
    if(ui->radioButton_english->isChecked()){
        m_pSetting->setValue("Setting/Language","English");
    }else{
        m_pSetting->setValue("Setting/Language","简体中文");
    }
}



//void VSettings::on_pushButton_apply_clicked()
//{
//    writeSettings();
//}

void VSettings::on_radioButton_zn_clicked(bool checked)
{
    Q_UNUSED(checked)
    m_pSetting->setValue("Setting/Language","简体中文");
    CUIHelper::showHintInfo(tr("重启生效"),this);
}

void VSettings::on_radioButton_english_clicked(bool checked)
{
    Q_UNUSED(checked)
    m_pSetting->setValue("Setting/Language","English");
    CUIHelper::showHintInfo(tr("重启生效"),this);
}

void VSettings::on_spinBox_port_valueChanged(int arg1)
{
    m_pSetting->setValue("Setting/DefaultPort",arg1);
}

void VSettings::on_comboBox_text_code_currentTextChanged(const QString &arg1)
{
    m_pSetting->setValue("Setting/TextCode",arg1);
    CMainWindow::getInstance()->slotTextCode(arg1);
}

void VSettings::on_spinBox_maxline_valueChanged(int arg1)
{
    m_pSetting->setValue("Setting/MaxLine",arg1);
}

void VSettings::on_spinBox_span_valueChanged(int arg1)
{
    m_pSetting->setValue("Setting/Span",arg1);
}
